/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Util;

import Database.ConnectToDatabase;
import java.sql.*;
import java.util.Vector;

/**
 *
 * @author Jack
 */
public class Producer {

    private int idproducer;
    private String nameproducer;

    /**
     * @return the idproduce
     */
    public int getIdproducer() {
        return idproducer;
    }

    /**
     * @param idproduce the idproduce to set
     */
    public void setIdproducer(int idproduce) {
        this.idproducer = idproduce;
    }

    /**
     * @return the nameproducer
     */
    public String getNameproducer() {
        return nameproducer;
    }

    /**
     * @param nameproducer the nameproducer to set
     */
    public void setNameproducer(String nameproducer) {
        this.nameproducer = nameproducer;
    }

    public static Vector<Producer> ListIDProducer() {
        Vector<Producer> listpdr = new Vector<Producer>();
        try {
            Connection con = ConnectToDatabase.getConnection();
            String query = "select *from Producer" + " order by idproducer desc";
            Statement st = con.createStatement();
            ResultSet rs = st.executeQuery(query);
            while (rs.next()) {
                Producer pdr = new Producer();
                pdr.setIdproducer(rs.getInt("idproducer"));
                pdr.setNameproducer(rs.getString("nameproducer"));
                listpdr.add(pdr);
            }

        } catch (Exception e) {
            listpdr = null;
        }
        return listpdr;
    }

    public static Vector<Producer> ListIDProducerOnId(int idproducer) {
        Vector<Producer> listpdr = new Vector<Producer>();
        try {
            Connection con = ConnectToDatabase.getConnection();
            String query = "select *from Producer where idproducer= " + idproducer + " order by idproducer desc";
            Statement st = con.createStatement();
            ResultSet rs = st.executeQuery(query);
            while (rs.next()) {
                Producer pdr = new Producer();
                pdr.setIdproducer(rs.getInt("idproducer"));
                pdr.setNameproducer(rs.getString("nameproducer"));
                listpdr.add(pdr);
            }

        } catch (Exception e) {
            listpdr = null;
        }
        return listpdr;
    }

    public static String ListNameProducer(int idpdr) {
        String listnpd = "";
        try {
            Connection con = ConnectToDatabase.getConnection();
            String query = "select nameproducer from Producer,Produce where Produce.idproducer=producer.idproducer and Produce.idProduce=" + idpdr;
            Statement st = con.createStatement();
            ResultSet rs = st.executeQuery(query);
            while (rs.next()) {
                listnpd = rs.getString("nameproducer");
            }
        } catch (Exception e) {
            listnpd = null;
            System.out.println(e);
        }
        return listnpd;
    }

    public boolean AddProducer() {
        return CommonQuery.Command("insert into Producer values('" + this.nameproducer + "')");
    }

    public boolean UpdateProducer() {
        return CommonQuery.Command("update Producer  set nameproducer='" + this.nameproducer + "' where idproducer=" + this.idproducer);
    }

    public boolean DeleteProducer() {
        return CommonQuery.Command("Delete Producer where idproducer=" + this.idproducer);
    }
}
